/**************************************************************************//**
 * @item     CosyOS-II Kernel
 * @file     sv_create.h
 * @brief    创建服务（用于创建各种内核对象）
 * @author   迟凯峰
 * @version  V1.0.1
 * @date     2023.10.25
 ******************************************************************************/

#ifndef __SV_CREATE_H
#define __SV_CREATE_H



/*
 * 声明任务
 */

/* 无私信 */
#define sCSV_ExternTask(name) \
	extern s_taskhand_ts u_taskhand_##name; \
	void name(void)

/* 有私信 */
#define sCSV_ExternTask_Msg(name) \
	extern s_taskhand_ts u_taskhand_##name; \
	void name(MCUCFG_TASKMSG_VAR, 



/*
 * 创建任务
 */

/* 创建一般任务 */
#if SYSCFG_DEBUGGING == __ENABLED__
	#if SYSCFG_TASKCREATEMODE == __STATIC__
		#define sCSV_CreateTask(name, pri, tss, srt, ndm) \
			MCUCFG_STACK_ALIGN static s_u8_t u_taskstack_##name[tss]; \
			s_taskhand_ts u_taskhand_##name = {0, 0, 0, OS_STATUS_DELETED, 0, NULL, u_taskstack_##name, tss, #name, {0}, MCUCFG_BASICSTACKSIZE, srt, 0, NULL, NULL, pri, (s_voidvoid_tfp)name}; \
			void name \
			sCreateTaskMsg_##ndm
			
	#elif SYSCFG_TASKCREATEMODE == __BALANCE__
		#define sCSV_CreateTask(name, pri, tss, srt, ndm) \
			s_taskhand_ts u_taskhand_##name = {0, 0, 0, OS_STATUS_DELETED, 0, NULL, NULL, tss, #name, {0}, MCUCFG_BASICSTACKSIZE, srt, 0, NULL, NULL, pri, (s_voidvoid_tfp)name, false}; \
			void name \
			sCreateTaskMsg_##ndm
			
	#elif SYSCFG_TASKCREATEMODE == __DYNAMIC__
		#define sCSV_CreateTask(name, pri, tss, srt, ndm) \
			s_taskhand_ts u_taskhand_##name = {0, tss, pri, (s_voidvoid_tfp)name, #name, srt, NULL}; \
			void name \
			sCreateTaskMsg_##ndm
			
	#endif
#else
	#if SYSCFG_TASKCREATEMODE == __STATIC__
		#define sCSV_CreateTask(name, pri, tss, srt, ndm) \
			MCUCFG_STACK_ALIGN static s_u8_t u_taskstack_##name[tss]; \
			s_taskhand_ts u_taskhand_##name = {0, 0, 0, OS_STATUS_DELETED, 0, NULL, u_taskstack_##name, tss, srt, 0, NULL, NULL, pri, (s_voidvoid_tfp)name}; \
			void name \
			sCreateTaskMsg_##ndm
			
	#elif SYSCFG_TASKCREATEMODE == __BALANCE__
		#define sCSV_CreateTask(name, pri, tss, srt, ndm) \
			s_taskhand_ts u_taskhand_##name = {0, 0, 0, OS_STATUS_DELETED, 0, NULL, NULL, tss, srt, 0, NULL, NULL, pri, (s_voidvoid_tfp)name}; \
			void name \
			sCreateTaskMsg_##ndm
			
	#elif SYSCFG_TASKCREATEMODE == __DYNAMIC__
		#define sCSV_CreateTask(name, pri, tss, srt, ndm) \
			s_taskhand_ts u_taskhand_##name = {0, tss, pri, (s_voidvoid_tfp)name, srt, NULL}; \
			void name \
			sCreateTaskMsg_##ndm
			
	#endif
#endif

/* 创建定时中断任务 */
#define sCSV_CreateTask_TimInt(tmid, arl, name, pri, tss, srt, ndm) \
	s_taskhand_ts u_taskhand_##name; \
	const s_timinthand_ts _CONST_MEM_ u_timinthand_##tmid = {&u_taskhand_##name, (s_voidvoid_tfp)0, arl}; \
	sCSV_CreateTask(name, pri, tss, srt, ndm)

/* 创建定时查询任务 */
#define sCSV_CreateTask_TimQry(tmid, event, arl, name, pri, tss, srt, ndm) \
	s_bool_t u_timqryevent_##tmid(void) MCUCFG_C51USING \
	{ \
		return (event ? true : false); \
	} \
	s_taskhand_ts u_taskhand_##name; \
	const s_timqryhand_ts _CONST_MEM_ u_timqryhand_##tmid = {&u_taskhand_##name, (s_voidvoid_tfp)0, (s_boolvoid_tfp)u_timqryevent_##tmid, arl}; \
	sCSV_CreateTask(name, pri, tss, srt, ndm)



/*
 * 创建钩子
 */

/* 创建定时中断钩子 */
#define sCSV_CreateHook_TimInt(tmid, arl, name) \
	void name(void); \
	const s_timinthand_ts _CONST_MEM_ u_timinthand_##tmid = {(s_taskhand_tsp)0, (s_voidvoid_tfp)name, arl}; \
	void name(void) MCUCFG_USING

/* 创建定时查询钩子 */
#define sCSV_CreateHook_TimQry(tmid, event, arl, name) \
	s_bool_t u_timqryevent_##tmid(void) MCUCFG_C51USING \
	{ \
		return (event ? true : false); \
	} \
	void name(void); \
	const s_timqryhand_ts _CONST_MEM_ u_timqryhand_##tmid = {(s_taskhand_tsp)0, (s_voidvoid_tfp)name, (s_boolvoid_tfp)u_timqryevent_##tmid, arl}; \
	void name(void) MCUCFG_USING



/*
 * 二值信号量
 */

/* 声明 */
#define	sCSV_ExternBin(name) \
	extern volatile s_bool_t name

/* 创建 */
#define	sCSV_CreateBin(name) \
	volatile s_bool_t name



/*
 * 互斥信号量
 */

/* 声明 */
#define	sCSV_ExternMut(name) \
	extern s_mutex_ts name

/* 创建 */
#define	sCSV_CreateMut(name) \
	s_mutex_ts name = {false, NULL, 0}



/*
 * 计数信号量
 */

/* 声明 */
#define	sCSV_ExternSem(name) \
	extern s_sem_ts name

/* 创建 */
#define	sCSV_CreateSem(name, init, max) \
	s_sem_ts name = {init, max}



/*
 * 飞信
 */

/* 声明信箱 */
#define sCSV_ExternFetion(name) \
	extern volatile m_fetion_t name

/* 创建信箱 */
#define sCSV_CreateFetion(name) \
	volatile m_fetion_t name = false



/*
 * 消息邮箱
 */

/* 声明邮箱 */
#define sCSV_ExternMailbox(name, type) \
	extern type name; \
	sCSV_ExternBin(name##_gnmf)

/* 创建邮箱 */
#define sCSV_CreateMailbox(name, type) \
	type name; \
	sCSV_CreateBin(name##_gnmf) = false



/*
 * 消息队列
 */

/* 声明静态消息队列 */
#define sCSV_ExternMsgQueue_Static(name) \
	extern s_msgqueue_static_ts name

/* 声明动态消息队列 */
#define sCSV_ExternMsgQueue_Dynamic(name) \
	extern s_msgqueue_dynamic_ts name

/* 创建静态消息队列 */
#define	sCSV_CreateMsgQueue_Static(name, mode, len) \
	void *u_msgqueue_##name[len]; \
	s_msgqueue_static_ts name = {0, __STATIC__, mode, len, true, u_msgqueue_##name, u_msgqueue_##name + len - 1, u_msgqueue_##name}

/* 创建动态消息队列 */
#define	sCSV_CreateMsgQueue_Dynamic(name, mode, len) \
	s_msgqueue_dynamic_ts name = {0, __DYNAMIC__, mode, len, true, NULL, NULL}



/*
 * 事件标志组
 */

/* 声明 */
#define sCSV_ExternFlagGroup(name, bits) \
	typedef struct{bits} u_##name##_ts; \
	extern volatile u_##name##_ts name

/* 创建 */
#define sCSV_CreateFlagGroup(name) \
	volatile u_##name##_ts name



/*
 * 线程内存
 */

/* 创建线程内存池 */
#define	sCSV_CreateMempool \
	s_thrmem_ts u_thrmem = {NULL, NULL, 0}



#endif
